<template>
  <van-row class="post-comment">
    <van-col span="20"
      ><van-field
        rows="2"
        v-model.trim="message"
        autosize
        type="textarea"
        maxlength="50"
        placeholder="留言"
        show-word-limit
    /></van-col>
    <van-col span="4">
      <van-button
        type="default"
        class="post-btn"
        size="mini"
        @click="onPost"
        :disabled="!message"
        >发布</van-button
      >
    </van-col>
  </van-row>
</template>

<script>
import { addComment } from '@/api/comments'
export default {
  name: 'PostComment',
  components: {},
  props: {
    target: {
      type: [Number, Object, String],
      required: true
    },
    articleId: {
      type: [Number, Object, String],
      default: null
    },
    replyTo: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      message: ''
    }
  },
  methods: {
    async onPost() {
      this.$toast.loading({
        forbidClick: true,
        message: '发布中'
      })
      const { data } = await addComment({
        target: this.target.toString(),
        content: this.message,
        art_id: this.articleId ? this.articleId.toString() : null
      })
      this.$emit('post-success', data.data.new_obj)
      this.$toast.success('发布成功')
      this.message = ''
    }
  }
}
</script>

<style lang="less" scoped>
.post-comment {
  padding: 12px;
  display: flex;
  align-items: center;
  .post-btn {
    font-size: 15px;
    text-align: center;
  }
}
</style>